package cn.wizzer.app.web.commons.ext.websocket.action.live;

import cn.wizzer.app.sys.modules.models.SysMsg;
import cn.wizzer.app.sys.modules.services.SysMsgService;
import cn.wizzer.app.user.modules.models.ChatReport;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.user.modules.services.ChatReportService;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.RedisUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.JSONUtil;
import cn.wizzer.notify.WkNotifyUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 * @author 22099
 */
@IocBean
@NettyAction(uri = "/report")
public class ReportAction extends BaseNettyAction {

    @Inject
    private RedisService redisService;

    @Reference
    @Inject
    ChatReportService chatReportService;

    @Reference
    @Inject
    private SysMsgService sysMsgService;

    @Inject
    @Reference
    private WkNotifyUtil wkNotifyUtil;


    public static BaseNettyAction instance() {
        return BaseNettyAction.instance(ReportAction.class);
    }

    @Override
    public Result action(Channel income, JSONObject params) {
        //用户信息
        String userStr = RedisUtil.hget(ChatConstant.CHAT_USER, income.id().toString());
        User user = JSONUtil.parseStringToJavaBean(User.class, userStr);

        String toUser = String.valueOf(params.get("userId"));
        String uris = String.valueOf(params.get("uris"));
        String content = String.valueOf(params.get("content"));

        ChatReport chatReport = new ChatReport();
        chatReport.setContent(content);
        chatReport.setImgUris(uris);
        chatReport.setToUser(toUser);
        chatReport.setCreator(user.getId());
        chatReport.setState(0);
        chatReportService.insert(chatReport);

        //构建消息通知
        SysMsg msg = sysMsgService.createMsg("system", "聊天举报提醒",
                String.format("<p>消息内容:您于<span style='color:red'>%s</span> 收到了一条举报记录，请注意点击前往查看。<a href=%s style='color:blue'>点击处理</a></p>", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"), "/platform/user/chat/report?id=" + chatReport.getId()),
                null);
        //插入通知
        sysMsgService.saveMsg(msg, null);
        //后台通知管理员
        wkNotifyUtil.notify(msg, null);
        return ChatResult.sysSend(ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.REMAND, "举报成功,后台受理中");
    }




}
